<template>
	<el-dialog title="选择条款" :visible.sync="dialogVisible" size="small" :close-on-press-escape="false" :before-close="off" :top="dialog_margin_top">
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;margin-top:0px;margin-bottom:5px;">
			<el-form :inline="true">
			  	<el-col :span="24">
			  		<div class="grid-content bg-purple-dark"></div>
					<el-form-item lable="条款代码"  label-width="150px" :span="3">
						<el-input v-model="formData.kindcode" size="small" placeholder="条款代码"></el-input>
					</el-form-item>
					<el-form-item lable="条款中文名称" label-width="150px" :span="3">
						<el-input v-model="formData.kindcname" size="small" placeholder="条款中文名称"></el-input>
					</el-form-item>
					<el-form-item>
						<el-button size="small" type="primary" @click="queryData()" icon="search">查询</el-button>
					</el-form-item>
					<el-form-item>
						<el-button size="small" @click="reset()"><i class="fa fa-undo"></i> 重置</el-button>
					</el-form-item>
			  	</el-col>
			</el-form>
		</el-col>

		<el-table :data="tableData" v-loading="loading" element-loading-text="数据加载中" ref="multipleTable" border style="width: 100%;" :height="tableHeight" :highlight-current-row="true" @sort-change="orderBy" @select="rowSelected" @row-click="rowSelect" @select-all="selectAll">
			<el-table-column type="selection" width="55"></el-table-column>
			<el-table-column prop="kindcode" label="条款代码" sortable="custom" width="140"></el-table-column>
			<el-table-column prop="kindcname" label="条款名称" sortable="custom" show-overflow-tooltip min-width="250"></el-table-column>
			<el-table-column prop="kindversion" label="条款版本" sortable="custom" width="120"></el-table-column>
		</el-table>

		<div class="block" style="float:right">
		    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
		      :current-page="formData.currentPage"
		      :page-sizes="[15, 30, 45]"
		      :page-size="formData.pageSize"
		      layout="total, sizes, prev, pager, next, jumper"
		      :total="total">
		    </el-pagination>
		  </div>

		<span slot="footer" class="dialog-footer">
			   <el-button type="default" @click="off">取消</el-button>
			   <el-button type="danger" @click="clear">清空已选条款</el-button>
			   <el-button type="warning" @click="choice">确认选择</el-button>
		</span>
	</el-dialog>
</template>

<script>
import KindAPI from "../../../../js/production/kind/kindAPI";
import dateUtil from "../../../../common/js/dateUtil";
import Utils from"../../../../js/production/utils";
export default {
  data () {
    	return {
    		dialogVisible: true,
    		formData: {
    		       currentPage: 1,
	                    pageSize: 15,
	                    kindcode: '',
	                    kindcname: '',
	                    kindversion: '',
	                    validstatus : '1',
	                    orderBy: '',
	                    order: '',
    		},
    		tableData: [],
    		total: 0,
		tableHeight: 0,
		isSortRequest: false,	//是否请求 
		selectedRows: [],
		dialog_margin_top: '2%',
		loading : false,
	}
  },
  mounted() {
  	this.queryData();
  	this.tableHeight = Utils.getScreenHeight()*0.6;
  	let vm = this;
  	document.onkeydown = function(e) {
  		if(e.keyCode == 13) {
  			vm.queryData();
  		}
  	}
  },
  methods: {
		queryData() {
			KindAPI.query(this);
		},
		handleSizeChange(val) {
			this.formData.pageSize = val;
			this.queryData();
		},
		handleCurrentChange(val) {
			this.formData.currentPage = val;
			this.queryData();
		},
		orderBy(column, prop, order) {
			if (typeof(column) != 'undefined') {
				this.formData.orderBy = column.prop;
				if (column.order == 'descending') {
					this.formData.order = 'desc';
				} else {
					this.formData.order = 'asc';
				}
			}
			if (this.isSortRequest) {
				this.isSortRequest = false;
				this.queryData();
			}
		},
		off() {
			this.$parent.showKindSelectDialog = false;
		},
		rowSelected(selection, row) {
			for(let i = 0;i < this.selectedRows.length; i++) {
				if(row.kindcode == this.selectedRows[i].kindcode) {
					let index = this.getIndex(row.kindcode);
					this.selectedRows.splice(index,1);
					return;
				}
			}
			this.selectedRows.push(row);
		},
		rowSelect(row) {
			this.$refs.multipleTable.toggleRowSelection(row);
			for(let i = 0;i < this.selectedRows.length; i++) {
				if(row.kindcode == this.selectedRows[i].kindcode) {
					let index = this.getIndex(row.kindcode);
					this.selectedRows.splice(index,1);
					return;
				}
			}
			this.selectedRows.push(row);
		},
		selectAll(selection) {
			let choices = [];
			choices = selection;
			if(choices.length == 0) {								//取消全选的情况下
				for(let k = 0; k < this.tableData.length; k++) {
					for(let n = 0; n < this.selectedRows.length; n++) {			
						if(this.tableData[k].kindcode == this.selectedRows[n].kindcode) {
							let index = this.getIndex(this.tableData[k].kindcode);
							this.selectedRows.splice(index,1);
							break;
						}
					}
				}
			} else {											//全选的情况下
				for(let i= 0; i < choices.length; i++) {
					if(this.selectedRows.length == 0) {					//第一次勾选直接push
						this.selectedRows.push(choices[i]);
						continue;
					}
					let isFound = true;
					for(let j = 0; j < this.selectedRows.length; j++) {			//跳过已被选中的checkbox
						if(choices[i].kindcode == this.selectedRows[j].kindcode) {
							isFound=false;
							break;
						}
					}
					if(isFound) {
						this.selectedRows.push(choices[i]);
					}
				}
			}
		},
		getIndex(val) {
			for(let i = 0; i < this.selectedRows.length; i++) {
				if(this.selectedRows[i].kindcode == val) {
					return i;
				}
			}
		},
		choice() {
			if (this.selectedRows.length < 1) {
		                        _this.$message({
		                            type: 'error',
		                            message: '请选择条款！'
		                        });
		                        return;
		              }
		              let kindcodes = '';
		              let kindcnames = '';
		              let kindversions = '';
		              let self = this;
		              this.selectedRows.forEach(function(row){
		              	kindcodes +=  row.kindcode + ",";
		              	kindcnames +=  row.kindcname + ",";
		              	kindversions +=  row.kindversion + ",";
		              	self.$parent.formData['img' + row.kindcode] = null;
		              });
		              this.$parent.formData.kindcode = kindcodes.substring(0,kindcodes.length-1);
		              this.$parent.formData.kindcname = kindcnames.substring(0,kindcnames.length-1);
		              this.$parent.formData.kindversion = kindversions.substring(0,kindversions.length-1);
		              this.$parent.kinds = this.selectedRows;
			this.off();
		},
		clear() {
			 this.$parent.formData.kindcode = '';
			 this.$parent.formData.kindcname = '';
			 this.$parent.formData.kindversion = '';
			 this.$parent.kinds = [];
			 this.off();
		},
		reset() {
			this.formData.kindcode = '';
			this.formData.kindcname = '';
			this.formData.currentPage = 1;
			this.queryData();
		},
  }
}
</script>